package br.com.fafica.sisape.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.com.fafica.sisape.conexao.ConnectionFactory;
import br.com.fafica.sisape.model.Internacao;

public class DAOInternacao implements InterfaceDAOInternacao{
	
	
	public void createInternacao(Internacao internacao, Integer cod) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "insert into Internacao "
				+ "(CodUsuario, DataEntradaInternacao, DataSaidaInternacao, Hospital_Clinica, Medico, MotivoInternacao) "
				+ "values (?,?,?,?,?,?)";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, cod);
			stmp.setString(2, internacao.getDataEntrada());
			stmp.setString(3, internacao.getDataSaida());
			stmp.setString(4, internacao.getHospitalClinica());
			stmp.setString(5, internacao.getMedico());
			stmp.setString(6, internacao.getMotivo());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

	
	public ArrayList<Internacao> readInternacao(Integer cod) throws SQLException {
		Connection con = new ConnectionFactory().getConnection();
		String seleciona = " where CodUsuario="+Integer.toString(cod);
		String sql = "Select * from Internacao"+seleciona;

		try {
			PreparedStatement stmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();

			ArrayList<Internacao> lista = new ArrayList<Internacao>();
			while (rs.next()) {
				Internacao internacao = new Internacao();
				internacao.setDataEntrada(rs.getString("DataEntradaInternacao"));
				internacao.setDataSaida(rs.getString("DataSaidaInternacao"));
				internacao.setHospitalClinica(rs.getString("Hospital_Clinica"));
				internacao.setMedico(rs.getString("Medico"));
				internacao.setMotivo(rs.getString("MotivoInternacao"));
				internacao.setCod(rs.getInt("idInternacao"));
				lista.add(internacao);
			}
			rs.close();
			stmt.close();
			return lista;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
		return null;
	}

	
	public void updateInternacao(Internacao internacao) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "update Internacao set DataEntradaInternacao=?"
				+ " ,DataSaidaInternacao=?"
				+ " ,Hospital_Clinica=?" + " ,Medico=?"
				+ " ,MotivoInternacao=?"
				+ "where idInternacao=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setString(1, internacao.getDataEntrada());
			stmp.setString(2, internacao.getDataSaida());
			stmp.setString(3, internacao.getHospitalClinica());
			stmp.setString(4, internacao.getMedico());
			stmp.setString(5, internacao.getMotivo());
			stmp.setInt(6, internacao.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Salvo com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}
	}

	
	public void deleteInternacao(Internacao internacao) throws SQLException {

		Connection con = new ConnectionFactory().getConnection();

		String sql = "delete from Internacao where idInternacao=?";
		try {

			PreparedStatement stmp = (PreparedStatement) con
					.prepareStatement(sql);
			stmp.setInt(1, internacao.getCod());
			stmp.execute();
			stmp.close();
			System.out.println("Deletado com Sucesso");
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} finally {
			con.close();
		}

	}

}
